package org.jeecg.modules.exKaibiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.exKaibiao.entity.ExBidOpeningRecord;
import org.jeecg.modules.exKaibiao.service.IExBidOpeningRecordService;
import org.jeecg.modules.exKaibiao.service.IExKaibiaoService;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.jeecg.modules.exTender.entity.ExTenderList;
import org.jeecg.modules.exTender.service.IExTenderListService;
import org.jeecg.modules.exZbidOpeningArrange.entity.ExZbidOpeningArrange;
import org.jeecg.modules.exZbidOpeningArrange.service.IExZbidOpeningArrangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangpf-d
 * @since 2024/12/17
 */
@Api(tags = "开标")
@RestController
@RequestMapping("/exKaibiao")
@Slf4j
public class ExKaibiaoController {
    @Autowired
    private IExSectionService exSectionService;
    @Autowired
    private IExTenderListService exTenderListService;
    @Autowired
    private IExKaibiaoService exKaibiaoService;
    @Autowired
    private IExBidOpeningRecordService exBidOpeningRecordService;
    @Autowired
    private IExZbidOpeningArrangeService exZbidOpeningArrangeService;
    @ApiOperation(value = "项目标段-全部数据", notes = "项目标段-全部数据")
    @GetMapping(value = "/exSection/listAll")
    public Result<List<Map>> listAll(@RequestParam(name = "projectId") String projectId) {
        List<Map> mapList = new ArrayList<>();
        List<ExSection> sectionList = exSectionService.selectByMainId(projectId);
        List<String> sectionIds = sectionList.stream().map(ExSection::getId).collect(Collectors.toList());
        if (sectionIds.isEmpty()) {
            return Result.OK(mapList);
        }
        exBidOpeningRecordService.syncRecordStatus(sectionIds);
        sectionList.forEach(item -> {
            ExBidOpeningRecord record = exBidOpeningRecordService.getBySectionId(item.getId());
            ExZbidOpeningArrange exZbidOpeningArrange = exZbidOpeningArrangeService.getBySectionId(item.getId());
            Map map = new HashMap();
            map.put("bidSectionId", item.getId());
            map.put("bidSectionName", item.getBidSectionName());
            map.put("bidSectionContent", item.getBidSectionContent());
            map.put("kaibiaoDate", exZbidOpeningArrange.getBidOpeningTime());//开标时间
            map.put("kaibiaoStatus", record == null ? "0" : "1");//开标状态
            mapList.add(map);
        });
        return Result.OK(mapList);
    }

    @ApiOperation(value = "投标人解密列表", notes = "投标人解密列表")
    @GetMapping(value = "/exTender/decryptList")
    public Result<List<Map>> exTenderDecryptList(@RequestParam(required = false) String bidSectionId) {
        LambdaQueryWrapper<ExTenderList> query = new LambdaQueryWrapper<ExTenderList>().eq(ExTenderList::getBidSectionId, bidSectionId);
        List<ExTenderList> exTenderLists = exTenderListService.list(query);
        List<Map> mapList = new ArrayList<>();
        exTenderLists.forEach(item -> {
            Map map = new HashMap();
            map.put("tenderListId", item.getId());
            map.put("bidderUserId", item.getBidderUserId());
            map.put("bidderName", item.getBidderName());
            map.put("decryptStatus", item.getDecryptStatus());//解密状态
            map.put("decryptDate", DateUtils.now()); //解密时间
            mapList.add(map);
        });
        return Result.OK(mapList);
    }

    @ApiOperation(value = "投标人详细列表", notes = "投标人详细列表")
    @GetMapping(value = "/exTender/detailList")
    public Result<List<Map>> exTenderDetailList(@RequestParam(required = false) String bidSectionId) {
        List<Map> datilList = exKaibiaoService.deailList(bidSectionId);
        return Result.OK(datilList);
    }

    @ApiOperation(value = "开标-开始", notes = "开标-开始")
    @PostMapping(value = "/start")
    public Result<Map> start(@RequestParam(required = false) String bidSectionId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ExBidOpeningRecord exBidOpeningRecord = exKaibiaoService.kaibiaoStart(bidSectionId);
        Map map = new HashMap();
        map.put("decryptEndTime", exBidOpeningRecord.getDecryptEndTime());
        map.put("status", exBidOpeningRecord.getStatus());
        return Result.ok(map);
    }

    @ApiOperation(value = "开标-完成", notes = "开标-完成")
    @PostMapping(value = "/complete")
    public Result<Boolean> complete(@RequestBody ExBidOpeningRecord record) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        exKaibiaoService.complete(record.getBidSectionId());
        return Result.ok(true);
    }


}
